﻿var express = require('express');
var router = express.Router();	
var db = require('../dao/tagDao');
var site_type =	config = require("../config").site_type;


exports.index = function(req, res, next){
    var condition = {
        keyword: req.query.keyword || '',
        title: req.query.title || ''
    };
	db.findAll(function (err, todos) {
        if (err) {
            return next(err);
        }
		res.render('index.html', { 
			pageTitle: 'Express',
			controllerName: "tag",
			siteType: site_type,
			curType: site_type.tag,
			todos: todos,
            searchCondition: condition
		});
    });
};

exports.edit = function (req, res, next) {
    var id = req.params.id;
    if(id == 0){
        res.render('edit.html', {
            siteType: site_type,
            curType: site_type.tag,
            todo: {_id:id, title:"", color:""}
        });
    }
    else{
        db.findById(id, function (err, doc) {
            if (err) {
                return next(err);
            }
            //console.log(doc)
            res.render('edit.html', {
                siteType: site_type,
                curType: site_type.tag,
                todo: doc
            });
        });
    }
};

exports.editSubmit = function (req, res, next) {
    var id = req.body.id || 0;
    
	var title = req.body.title || '';
    var className = req.body.className || '';
    var color = req.body.color || '';
	title = title.trim();
    if (!title) {
        //return res.render('error.html', {message: '标题是必须的'});
        res.json({status:'0', msg:'标题是必须的'});
    }
    db.findExistByTitle(id, {title: title}, function (err, doc) {
        if (err) {
            return next(err);
        }
        else {
            console.log(doc)
            if(doc == null || doc.title != title){              
                if(id == 0){
                    db.add( 
                        {
                            title: title,
                            color: color,
                            class_name: className
                        }, function (err, row) {
                        if (err) {
                            return next(err);
                        }
                        res.json({status:'1'});
                    });
                }
                else{
                    db.editTitle( 
                        {
                            _id: id,
                            title: title,
                            color: color,
                            class_name: className
                        }, function (err, row) {
                        if (err) {
                            return next(err);
                        }
                        res.json({status:'1'});
                    });
                }
            }
            else{
                res.json({status:'0', msg: '标题已经存在了'});
            }
        }
    }); 

};

exports.remove = function (req, res, next) {
    var id = req.params.id;

    db.remove(id, function (err, doc) {
        if (err) {
            return next(err);
        }    

		res.json({status:'1'});        
    });

};

